package com.tale.ssm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.util.StringUtil;
import com.tale.ssm.entity.Clazz;
import com.tale.ssm.entity.Grade;
import com.tale.ssm.service.ClazzService;
import com.tale.ssm.service.GradeService;
import com.tale.ssm.vo.Page;
import net.sf.json.JSONArray;

@Controller
@RequestMapping("/clazz")
public class ClazzController {
	
	@Autowired
	private ClazzService service;
	@Autowired
	private GradeService gradeService;
	
	@GetMapping("/list")
	public ModelAndView index(ModelAndView model) {
		model.setViewName("clazz/clazz_list");
		List<Grade> list = gradeService.findAll();
		model.addObject("gradeList", list);
		model.addObject("gradeListJson", JSONArray.fromObject(list));
		
		return model;
	}
	
	@PostMapping("/get_list")
	@ResponseBody
	public Map<String, Object> list(
			@RequestParam(value = "name", defaultValue = "", required = false)String name,
			@RequestParam(value = "gradeId", required = false)Long gradeId,
			Page page
			){
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> query = new HashMap<String, Object>();
		query.put("name", "%" + name + "%");
		if(gradeId != null){
			query.put("gradeId", gradeId);
		}
		query.put("gradeId", gradeId);
		query.put("offset", page.getOffset());
		query.put("pageSize", page.getRows());
		map.put("rows", service.findList(query));
		map.put("total", service.getTotal(query));
		
		return map;
	}
	
	@PostMapping("/edit")
	@ResponseBody
	public Map<String, String> edit(
			Clazz clazz
			){
		Map<String, String> map = new HashMap<String, String>();
		if(service.edit(clazz) <= 0) {
			map.put("type", "error");
			map.put("msg", "修改失败");
			return map;
			
		}
		map.put("type", "success");
		map.put("msg", "修改成功");
		
		return map;
	}

	@PostMapping("/delete")
	@ResponseBody
	public Map<String, String> delete(
			@RequestParam(value = "ids[]")Long[] ids
			){
		Map<String, String> map = new HashMap<String, String>();
		
		String id = "";
		for(Long i:ids) {
			id += i + ",";
		}
		id = id.substring(0, id.length() - 1);
		if(service.delete(id) <= 0) {
			map.put("type", "error");
			map.put("msg", "删除失败");
			return map;
		}
		map.put("type", "success");
		
		return map;
	}
	
	
	@PostMapping("/add")
	@ResponseBody
	public Map<String, String> add(Clazz clazz){
		Map<String, String> map = new HashMap<String, String>();
		map.put("type", "error");
		if(StringUtil.isEmpty(clazz.getName())) {
			map.put("msg", "空");
			return map;
		}
		
		if(service.add(clazz) <= 0) {
			map.put("msg", "添加失败");
			return map;
		}
		map.put("type", "success");
		map.put("msg", "添加成功");
		
		return map;
	}
	
}
